//==================================================================
/// CppKazRace_Events.cpp
///
/// Created by Davide Pasca - 2009/10/12
/// See the file "license.txt" that comes with this project for
/// copyright info. 
//==================================================================

#include "stdafx.h"
#include "CppKazRace.h"

//==================================================================
void CppKazRace::onResize( U32 w, U32 h )
{
	if (mCurWinSize.width	== w &&
		mCurWinSize.height	== h )
	{
		return;
	}

	mCurWinSize.width	= w;
	mCurWinSize.height	= h;

	mpoOffGfx->resize( w, h );

	// setup global stuff
	//mScreenD = d.width * 60 / 100;
	mScreenD = (float)w * 70 / 100;

	mpoMyCarSptr->mZ = (int)(mScreenD * 160 / 100);

	mpoXFormer->Resize( w, h, mScreenD );

	mpoHor->Resize( w, h );

	mpoRoad->Resize( w, h, 250, 100 );
}

//==================================================================
void CppKazRace::onPaint( HWND hWnd )
{
	mypaint();
	mOffGfxIsBGR = false;

	HDC			hdc;
	PAINTSTRUCT	ps;

	hdc = BeginPaint( hWnd, &ps );

	char		wbinfo[sizeof(BITMAPINFO)+sizeof(DWORD)*3];
	BITMAPINFO	*pBMInfo;

	pBMInfo = (BITMAPINFO *)wbinfo;
	memset( pBMInfo, 0, sizeof(BITMAPINFO) );
	pBMInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
	pBMInfo->bmiHeader.biWidth		= mpoOffGfx->mW;
	pBMInfo->bmiHeader.biHeight		= -(int)mpoOffGfx->mH;
	pBMInfo->bmiHeader.biPlanes		= 1;
	pBMInfo->bmiHeader.biBitCount	= 32;
	pBMInfo->bmiHeader.biSizeImage	= mpoOffGfx->mW * mpoOffGfx->mH * 4;
	pBMInfo->bmiHeader.biXPelsPerMeter = 75;
	pBMInfo->bmiHeader.biYPelsPerMeter = 75;
	pBMInfo->bmiHeader.biClrUsed		= 0;
	pBMInfo->bmiHeader.biClrImportant	= 0;

	pBMInfo->bmiHeader.biCompression = BI_RGB;

	if NOT( mOffGfxIsBGR )
	{
		for (U32 y=0; y < (U32)mpoOffGfx->mH; ++y)
		{
			U8	*pRow = mpoOffGfx->mpoImg->GetPixelPtr( 0, y );

			for (U32 x=0; x < (U32)mpoOffGfx->mW; ++x)
			{
				U8 tmp = pRow[0];
				pRow[0] = pRow[2];
				pRow[2] = tmp;
				pRow += 4;
			}
		}

		mOffGfxIsBGR = true;
	}

	SetDIBitsToDevice(
		hdc, 0, 0,
		mpoOffGfx->mW,
		mpoOffGfx->mH,
		0,
		0,//mpoOffGfx->mH-1,
		0,
		mpoOffGfx->mH,
		&mpoOffGfx->mpoImg->GetPInt()[0],
		pBMInfo,
		DIB_RGB_COLORS );


	EndPaint( hWnd, &ps );
}

//==================================================================
bool CppKazRace::EvenCB( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
{
	switch ( message )
	{
	case WM_SIZE:
		onResize( LOWORD( lParam ), HIWORD( lParam ) );
		SetTimer( hWnd, 0, 1, NULL );
		break;

	case WM_TIMER:
		animate();
		InvalidateRect( hWnd, NULL, FALSE );
		//mypaint();
		_laptimes[0] += 33;

		SetTimer( hWnd, 0, 1, NULL );
		break;

	case WM_PAINT:
		onPaint( hWnd );
		return true;


	case WM_KEYDOWN:
		switch ( wParam )
		{
		case VK_LEFT:	mLeftIsDown = true; break;
		case VK_RIGHT:	mRightIsDown = true; break;
		case 'a':
		case VK_UP:		mUpIsDown = true; break;	
		case ' ':
		case VK_DOWN:	mDownIsDown = true; break;
		}
		break;

	case WM_KEYUP:
		switch ( wParam )
		{
		case VK_LEFT:	mLeftIsDown = false; break;
		case VK_RIGHT:	mRightIsDown = false; break;
		case 'a':
		case VK_UP:		mUpIsDown = false; break;	
		case ' ':
		case VK_DOWN:	mDownIsDown = false; break;
		}
		break;
	}

	return false;
}

